const {BaseService} = require('../../basic.js')
const {
	ProjectModel
} = require('dx-func-entity')
const _ = require('underscore');
// const {
// 	buildMenus
// } = require('../util/util.js')

module.exports = class Project extends BaseService {
	constructor(ctx) {
		super(ctx);
		this.entity = ProjectModel;		
	}
	afterDelete({_ids=[]}){
		return this.$dx.service.candidate.where('project_id','in',_ids).delete();
	}
	// beforeUpdate(data){
		
	// 	return new Promise((re,rj)=>{
	// 		setTimeout(()=>{
	// 			data.name = '蒋秀川测试4432'
	// 			re(data);
	// 		},3000)
	// 	})
	// }
	company() {
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-company', 'company_id', '_id').bindField({
			company_name: 'name'
		});
	}
	hr() {
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-talent', 'hr_id', '_id').bindField({
			hr_name: 'name'
		});
	}
	findByIds(_ids){
		return this.where('_id','IN',_ids).field('name').limit(1000).select();
	}
	async findByNames(names=[]){
		const list = await this.where({name:['in',names]}).field('name').limit(1000).select();
		if(list.length==1000){
			const lst2 = await this.where({name:['in',names]}).skip(1000).field('name').limit(1000).select();
			list.push(...lst2);
		}
		return list;
	}
	async getProject({_id}){
		if(!_id){
			return this.$dx.error('项目 ID 不能为空');
		}
		const project = await this.where({_id}).with(['company']).find();
		return project
		// const where = {
		// 	'company_list.teacher':['contain',_id]
		// };
		// const talent = await this.$dx.service..where(where).with(['company']).limit(1000).select();
		// teacher.student = talent;
		// return teacher;
	}
	async getByTalentId({_id}){
		const list = await this.$dx.service.candidate.where({talent_id:_id}).limit(1000).select();
		const _ids = _.pluck(list,'project_id');
		const projects = await this.where('_id','IN',_ids).with('company').limit(1000).select(); 
		return projects.map(project=>{
			const candidate = _.findWhere(list,{project_id:project._id});
			candidate&&(project.talent_promot_list = candidate.promot_list);
			project.candidate = candidate;
			return project;
		})
	}
	async getByCompany({_cid}){
		return await this.where({company_id:_cid}).with(['company','hr']).limit(1000).select();
	}
	async findByJId(jid){
		const project = await this.where({job_id:jid}).find();
		return project;
	}
	async importFromJob({_id,force=false}){
		if(!force){
			const project = await this.findByJId(_id);
			if(project){
				return this.$dx.error('该职位已在项目中存在',project,2000);
			}
		}
		const job = await this.$dx.service.job.findById(_id);
		if(!job){
			return this.$dx.error('导入项目失败');
		}
		const params = {
			job_id:job._id,
			name:job.name,
			location:job.location,
			salary:job.salary,
			company_id:job.company_id,
			description:job.description,
			hr_id:job.hr_id,
			hr_name:job.hr_name,
			contarct:job.company_contract||job.company_email||'',
			tag:job.tag,
			link:job.link,
			status:0,
			from:job.from	
		}
		const _pid  = await this.insert(params);
		return this.$dx.success('导入成功',_pid);
	}
	async updateMerge({_id,_ids}){
		await this.where({'company_id':['in',_ids]}).update({
			company_id:_id
		});
		const result = await this.where({'target_ids':['in',_ids]}).field('name,target_ids').limit(100).select();
		if(result.length==0)return true;
		const list = _.map(result,project=>{
			const ids = _.without(project.target_ids,..._ids);
			return {
				_id:project._id,
				target_ids:[...ids,_id]
			}
		});
		for(let i = 0;i<list.length;i++){
		    await this.update(list[i]);
		}
		return true;
	}
	async updateTMerge({_id,_ids}){
		const result = await this.where({'hr_id':['in',_ids]}).update({
			hr_id:_id
		});
		return result;
	}
	async addProjectTarget({project_id,name}){
		if(!project_id||!name){
			return this.$dx.error('信息不完整');
		}
		const project = await this.where({_id:project_id}).find();
		const target_names = project.target_names||[];
		if(target_names.includes(name)){
			return this.$dx.error('目标企业已存在');
		}
		target_names.push(name);
		const result = await this.where({_id:project_id}).update({target_names})
		return this.$dx.success('添加成功',result);
		
	}
}
